<template>
  <div>
    <div class="divider-content-only">
      <a-divider type="vertical" class="divider-only"/>
      基本费用项-账单-押金
    </div>
    <a-card :bordered="false">
      <div>
        <a-button type="primary" icon="plus" @click="$refs.addForm.add()">新增費用類別</a-button>
      </div>
      <s-table
        ref="table"
        size="default"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <span slot="countscopedSlots" slot-scope="text">
          {{ 'rember_count' | dictType(text) }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a  @click="$refs.editForm.edit(record)">编辑</a>
          <a-divider type="vertical" />
          <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => busCostBillDepositDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form ref="addForm" @ok="handleOk" />
      <edit-form ref="editForm" @ok="handleOk" />
    </a-card>
    <div class="divider-content-only divider-content-center-only">
      <a-divider type="vertical" class="divider-only"/>
      基本费用项-账单-水电物业
    </div>
    <a-card :bordered="false">
      <div>
        <a-button type="primary" icon="plus" @click="$refs.addFormDropower.add()">添加費用</a-button>
      </div>
      <s-table
        ref="tableDropower"
        size="default"
        :columns="columnsDropower"
        :data="loadDataDropower"
        :alert="true"
        :rowKey="(record) => record.id"
        :rowSelection="{ selectedRowKeys: selectedRowKeysDropower, onChange: onSelectChangeDropower }"
      >
        <span slot="typescopedSlots" slot-scope="text">
          {{ 'cost_type' | dictType(text) }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a  @click="$refs.editFormDropower.edit(record)">编辑</a>
          <a-divider type="vertical"/>
          <a-popconfirm  placement="topRight" title="确认删除？" @confirm="() => busCostBillHydropowerDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form-dropower ref="addFormDropower" @ok="handleOkDropower" />
      <edit-form-dropower ref="editFormDropower" @ok="handleOkDropower" />
    </a-card>
  </div>
</template>
<script>
  import { STable } from '@/components'
  import { busCostBillDepositPage, busCostBillDepositDelete } from '@/api/modular/main/buscostbilldeposit/busCostBillDepositManage'
  import addForm from './addForm.vue'
  import editForm from './editForm.vue'
  import { busCostBillHydropowerPage, busCostBillHydropowerDelete } from '@/api/modular/main/buscostbillhydropower/busCostBillHydropowerManage'
  import addFormDropower from './addFormDropower.vue'
  import editFormDropower from './editFormDropower.vue'
  export default {
    components: {
      STable,
      addForm,
      editForm,
      addFormDropower,
      editFormDropower
    },
    data () {
      return {
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '押金名称',
            align: 'center',
            dataIndex: 'depositName'
          },
          {
            title: '计数',
            align: 'center',
            dataIndex: 'count',
            scopedSlots: { customRender: 'countscopedSlots' }
          }
        ],
        columnsDropower: [
          {
            title: '名称',
            align: 'center',
            dataIndex: 'name'
          },
          {
            title: '类型',
            align: 'center',
            dataIndex: 'type',
            scopedSlots: { customRender: 'typescopedSlots' }
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return busCostBillDepositPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        // 加载数据方法 必须为 Promise 对象
        loadDataDropower: parameter => {
          return busCostBillHydropowerPage(Object.assign(parameter, this.queryParam)).then((res) => {
            return res.data
          })
        },
        countData: [],
        selectedRowKeys: [],
        selectedRows: [],
        selectedRowKeysDropower: [],
        selectedRowsDropower: []
      }
    },
    created () {
      this.columns.push({
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
      })
      this.columnsDropower.push({
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
      })
      const countOption = this.$options
      this.countData = countOption.filters['dictData']('rember_count')
    },
    methods: {
      busCostBillDepositDelete (record) {
        busCostBillDepositDelete(record).then((res) => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.$refs.table.refresh()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      handleOk () {
        this.$refs.table.refresh()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      busCostBillHydropowerDelete (record) {
        busCostBillHydropowerDelete(record).then((res) => {
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.$refs.tableDropower.refresh()
          } else {
            this.$message.error('删除失败') // + res.message
          }
        })
      },
      handleOkDropower () {
        this.$refs.tableDropower.refresh()
      },
      onSelectChangeDropower (selectedRowKeys, selectedRows) {
        this.selectedRowKeysDropower = selectedRowKeys
        this.selectedRowsDropower = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
  .divider-only{
    background-color: aqua;
  }
  .divider-content-only{
    font-size: 20px;
    margin-bottom: 10px;
  }
  .divider-content-center-only{
      margin-top: 10px;
  }
</style>
